package com.xuecheng.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {

    @Value("${xuecheng.elasticsearch.hostlist}")
    private String hostList;

    /**
     * RestHighLevelClient:在项目启动时，创建并存入容器
     */
    @Bean
    public RestHighLevelClient client() {
        //1、主机地址
        HttpHost httpHost = HttpHost.create(hostList);
        //2、构造器
        RestClientBuilder builder = RestClient.builder(httpHost);
        //3、创建客户端对象
        return new RestHighLevelClient(builder);
    }
}